/**
 * <p>显示数据的grid</p>
 * xtype = cceafviewgridpanel

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.ViewGridPanel
 * @extends Ext.grid.GridPanel
 * @since 1.0
 */
Ext.define('cceaf.control.ViewGridPanel',{
	extend: 'Ext.grid.Panel', 
	requires: [
       'cceaf.control.Button'
    ],
    alias: 'widget.cceafviewgridpanel',

	constructor: function(config){
		config = config || {};
		
		cceaf.control.ViewGridPanel.superclass.constructor.apply(this, arguments);
	},
	
	/**
	 * 是否显示分页栏，默认显示
	 *
     * @property hasPagingToolbar 
     * @type boolean
     * @default true
     */
	hasPagingToolbar: true,
	
	/**
	 * 列表的model
	 *
     * @property model 
     * @type String
     * @default null
     */
	model: null,
	
	/**
	 * 打印查询列表的action
	 *
     * @property printAction 
     * @type String
     * @default null
     */
	printAction: null,
	
	/**
	 * 查询的action
	 *
     * @property queryAction 
     * @type String
     * @default null
     */
	queryAction: null,
	
	/**
	 * 导出excel的action
	 *
     * @property exportExcelAction 
     * @type String
     * @default null
     */
	exportExcelAction: null,
	
	/**
	 * 是否存在close按钮，true则存在
	 *
     * @property closeButton 
     * @type Boolean
     * @default true
     */
	closeButton: true, 
	
	/**
	 * 设置每行的class
	 */
	viewConfig: {
		forceFit:true,
		enableRowBody:true,
		getRowClass : function(record, rowIndex, p, ds){
			return '';
		}
	},
	
	initComponent : function(){
		var config = this;
		
		var columns = config.columns;
		if (!columns) {
			cceaf.util.ExtHelper.showErrorDialog("必须设置grid的columns！");
		}

		var model = config.model;
		var queryAction =  config.queryAction;
		
		//定义数据源
		var ds;
		if (config.store) {
			ds = config.store;
		} else {
		    ds = new Ext.data.Store({
		    	proxy: {
		    		type: 'ajax',
		    		url: queryAction,
		    		reader: {
						type: 'json',
						root: 'data',
						totalProperty: 'totalCount',
						messageProperty: 'message'
					}
				},
				
		        listeners: {
		        	'beforeload': function(){
		        		if (model) {
		        			if (!Ext.ModelManager.getModel(model)) {
		        				//cceaf.util.ExtHelper.showErrorDialog("model:" + model + "不存在，请设置正确的model！");
		        				alert("model:" + model + "不存在，请设置正确的model！");
		        				return false;
		            		} 
		        		} else {
		        			//cceaf.util.ExtHelper.showErrorDialog("请定义对应的ViewModel！");
		        			alert("请定义对应的ViewModel！");
		        			return false;
		        		}
		        		return true;
		        	},
	        	
		        	'load': function(){
		        		var jr = ds.getProxy().getReader();
		        		if(!jr.jsonData) {
		        			cceaf.util.ExtHelper.showErrorDialog("没有设置正确的action或没有返回正确的json字符串！");
		        			return;
		        		}
		        		
		        		var success = jr.jsonData.success;
		        		var message = jr.jsonData.message;
		        		var url = jr.jsonData.url;
		        		if (success) {
		        			cceaf.util.ExtHelper.showInfoDialog(message, url);
		        		} else {
		        			cceaf.util.ExtHelper.showErrorDialog(message, url);
		        		}
		        	},
		        	
		        	'loadexception': function(){
		        		var jr = ds.getProxy().getReader();
		        		if(jr.jsonData){
			        		var message = jr.jsonData.message;
			        		var url = jr.jsonData.url;
			        		cceaf.util.ExtHelper.showErrorDialog(message,url);
		        		}
		        		else{
		        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
		        		}
		        	}
		        },
		        
		        autoDestroy: true,  
		    	pageSize: szpshang.Global.PAGE_SIZE,
		    	model: model
		    });
        
        }
        
        
        var items = [];
        var i = 0;
        if (config.exportExcelAction || config.printAction || config.closeButton) {
			if (config.exportExcelAction) {        	
        		items[i] = {itemId: 'btnExportExcel', xtype: 'cceafbutton', text: ' 导出XLS ', iconCls: 'exportExcelIcon'};
        		i++;
        	}
        	
        	items[i] = '-';
        	i++;
        	
			if (config.printAction) {        	
        		items[i] = {itemId: 'btnPrint', xtype: 'cceafbutton', iconCls: 'printListIcon', text: ' 打印 '};
        		i++;
        	}
        	
        	items[i] = '-';
        	i++;
        	
        	if (config.closeButton) {        	
        		items[i] = {itemId: 'btnClose', xtype: 'cceafbutton', iconCls: 'closeIcon', text: ' 关闭 '};
        		i++;
        	}
        }
        
	    var dockedItems = [{
        	xtype: 'toolbar',
			dock: 'top', 
			items: items
		}];
		
		var pagingtoolbar = {
			xtype: 'pagingtoolbar',
			store: ds,
	        dock: 'bottom',  
	        displayInfo: true,
	        displayMsg:'当前显示 {0} - {1}条记录 /共 {2}条记录',
	      	emptyMsg:'无显示数据',
	      	doRefresh : function(){
	            var me = this,
	                current = me.store.currentPage;

	            if (me.fireEvent('beforechange', me, current) !== false) {
	            	var gridpanel = me.up('gridpanel');
	            	var params = gridpanel.buildStoreParams();
	                me.store.loadPage(current, {params: params});
	            }
	        }
		};
		
		//dockedItems[dockedItems.length] = pagingtoolbar;
		if (config.hasPagingToolbar === true) {
			dockedItems[dockedItems.length] = pagingtoolbar;
		}
		
		Ext.applyIf(this, {
			store: ds,
	        border: true,
	        frame: true,
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        dockedItems:dockedItems
		});
		
		cceaf.control.ViewGridPanel.superclass.initComponent.call(this);
    },

    initEvents: function() {
    	cceaf.control.ViewGridPanel.superclass.initEvents.call(this);
    	
    	//注册导出按钮事件
    	if(this.down('#btnExportExcel')){
    		this.down('#btnExportExcel').on('click',this.exportExcel);
    	}
    	
    	//注册打印按钮事件
    	if(this.down('#btnPrint')){
    		this.down('#btnPrint').on('click',this.print);
    	}
    	
    	//注册关闭按钮事件
    	if(this.down('#btnClose')){
    		this.down('#btnClose').on('click',this.close);
    	}
    	
    	//注册其他事件
    	this.registerEvent();
    },
	
	/**
     * 导出到Excel
     */
    exportExcel: function(){
    	var viewGrid = this.findParentByType('cceafviewgridpanel');
    	var url =  viewGrid.exportExcelAction;
    	
    	cceaf.util.CceafHelper.openUrl(url);
    },
    
    /**
     * 打印页面
     */
    print: function(){
    	var viewGrid = this.findParentByType('cceafviewgridpanel');
    	var url =  viewGrid.printAction;
    	
    	cceaf.util.CceafHelper.openUrl(url);
    },
    
    /**
     * 关闭窗口
     */
    close: function(){
    	var viewWin = this.up('window');
    	if (viewWin) {
    		viewWin.close();
    	}
    },
    
    /**
     * 注册其他事件
     */
    registerEvent: function(){
    	
    },
    
    /**
     * 构建store的参数，在分页控件刷新时加载
     * 
     * @return {Object} store的过滤参数，例如：
     * {
     * 		param1: p1,
     * 		param2: p2
     * }
     */
    buildStoreParams: function () {
    	return null;
    }
    
    
});